using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Resources;
using System.Windows.Data;

namespace Infralution.Localization.Wpf
{
	public class ResourceEnumConverter : EnumConverter, IValueConverter
	{
		private class Class227 : Dictionary<string, object>
		{
		}

		private Dictionary<CultureInfo, Class227> dictionary_0 = new Dictionary<CultureInfo, Class227>();

		private ResourceManager resourceManager_0;

		private bool bool_0;

		private Array array_0;

		private Class227 method_0(CultureInfo cultureInfo_0)
		{
			Class227 value = null;
			if (cultureInfo_0 == null)
			{
				cultureInfo_0 = CultureInfo.CurrentCulture;
			}
			if (!dictionary_0.TryGetValue(cultureInfo_0, out value))
			{
				value = new Class227();
				foreach (object standardValue in GetStandardValues())
				{
					string text = method_1(cultureInfo_0, standardValue);
					if (text != null)
					{
						value.Add(text, standardValue);
					}
				}
				dictionary_0.Add(cultureInfo_0, value);
			}
			return value;
		}

		protected virtual string GetResourceName(object value)
		{
			Type type = value.GetType();
			return $"{type.Name}_{value.ToString()}";
		}

		private string method_1(CultureInfo cultureInfo_0, object object_0)
		{
			string resourceName = GetResourceName(object_0);
			string text = resourceManager_0.GetString(resourceName, cultureInfo_0);
			if (text == null)
			{
				text = resourceName;
			}
			return text;
		}

		private bool method_2(ulong ulong_0)
		{
			return ulong_0 switch
			{
				1uL => true, 
				0uL => false, 
				_ => (ulong_0 & (ulong_0 - 1L)) == 0L, 
			};
		}

		private string method_3(CultureInfo cultureInfo_0, object object_0)
		{
			if (Enum.IsDefined(object_0.GetType(), object_0))
			{
				return method_1(cultureInfo_0, object_0);
			}
			ulong num = Convert.ToUInt32(object_0);
			string text = null;
			foreach (object item in array_0)
			{
				ulong num2 = Convert.ToUInt32(item);
				if (method_2(num2) && (num2 & num) == num2)
				{
					string text2 = method_1(cultureInfo_0, item);
					text = ((text != null) ? $"{text}, {text2}" : text2);
				}
			}
			return text;
		}

		private object method_4(CultureInfo cultureInfo_0, string string_0)
		{
			Class227 @class = method_0(cultureInfo_0);
			object value = null;
			@class.TryGetValue(string_0, out value);
			return value;
		}

		private object method_5(CultureInfo cultureInfo_0, string string_0)
		{
			Class227 @class = method_0(cultureInfo_0);
			string[] array = string_0.Split(',');
			ulong num = 0uL;
			string[] array2 = array;
			int num2 = 0;
			while (true)
			{
				if (num2 < array2.Length)
				{
					string obj = array2[num2];
					object value = null;
					string key = obj.Trim();
					if (!@class.TryGetValue(key, out value))
					{
						break;
					}
					num |= Convert.ToUInt32(value);
					num2++;
					continue;
				}
				return Enum.ToObject(base.EnumType, num);
			}
			return null;
		}

		public ResourceEnumConverter(Type type, ResourceManager resourceManager)
			: base(type)
		{
			resourceManager_0 = resourceManager;
			object[] customAttributes = type.GetCustomAttributes(typeof(FlagsAttribute), inherit: true);
			bool_0 = customAttributes.Length != 0;
			if (bool_0)
			{
				array_0 = Enum.GetValues(type);
			}
		}

		public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
		{
			if (culture == null)
			{
				culture = CultureInfo.CurrentCulture;
			}
			if (value is string)
			{
				object obj = (bool_0 ? method_5(culture, (string)value) : method_4(culture, (string)value));
				if (obj == null)
				{
					obj = base.ConvertFrom(context, culture, value);
				}
				return obj;
			}
			return base.ConvertFrom(context, culture, value);
		}

		public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
		{
			if (culture == null)
			{
				culture = CultureInfo.CurrentCulture;
			}
			if (value != null && destinationType == typeof(string))
			{
				if (!bool_0)
				{
					return method_1(culture, value);
				}
				return method_3(culture, value);
			}
			return base.ConvertTo(context, culture, value, destinationType);
		}

		public static string ConvertToString(Enum value)
		{
			return TypeDescriptor.GetConverter(value.GetType()).ConvertToString(value);
		}

		public static List<KeyValuePair<Enum, string>> GetValues(Type enumType, CultureInfo culture)
		{
			List<KeyValuePair<Enum, string>> list = new List<KeyValuePair<Enum, string>>();
			TypeConverter converter = TypeDescriptor.GetConverter(enumType);
			foreach (Enum value in Enum.GetValues(enumType))
			{
				KeyValuePair<Enum, string> item = new KeyValuePair<Enum, string>(value, converter.ConvertToString(null, culture, value));
				list.Add(item);
			}
			return list;
		}

		public static List<KeyValuePair<Enum, string>> GetValues(Type enumType)
		{
			return GetValues(enumType, CultureInfo.CurrentUICulture);
		}

		object IValueConverter.Convert(object value, Type targetType, object parameter, CultureInfo culture)
		{
			return ConvertTo(null, culture, value, targetType);
		}

		object IValueConverter.ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
		{
			return ConvertFrom(null, culture, value);
		}
	}
}
